﻿Public Class ColorSet
    Private mColors(16) As Color
    Private Shared mColorNames(16) As String

    Shared Sub New()
        mColorNames(0) = "Black"
        mColorNames(1) = "Blue"
        mColorNames(2) = "Green"
        mColorNames(3) = "Cyan"
        mColorNames(4) = "Red"
        mColorNames(5) = "Magenta"
        mColorNames(6) = "Brown"
        mColorNames(7) = "Light gray"
        mColorNames(8) = "Dark Gray"
        mColorNames(9) = "Light Blue"
        mColorNames(10) = "Light Green"
        mColorNames(11) = "Light Cyan"
        mColorNames(12) = "Light Red"
        mColorNames(13) = "Light Magenta"
        mColorNames(14) = "Yellow"
        mColorNames(15) = "White"
    End Sub

    Public Shared Function FromFile(path As String) As ColorSet
        Dim ret As New ColorSet

        Try
            Dim reader As New RawReader(path)
            Do While reader.ReadNextToken
                ret.ParseColorToken(reader.CurrentToken)
            Loop
        Catch ex As Exception

        End Try

        Return ret
    End Function

    Private Sub ParseColorToken(token As Token)
        Dim parts() As String = token.Name.Split("_"c)
        Dim name As String = parts(0)
        Dim channel As String = parts(1)
        SetColorChannel(name, channel, CByte(token.Arguments(0)))
    End Sub

    Public Sub New()
        mColors(0) = Drawing.Color.FromArgb(0, 0, 0)
        mColors(1) = Drawing.Color.FromArgb(0, 0, 128)
        mColors(2) = Drawing.Color.FromArgb(0, 128, 0)
        mColors(3) = Drawing.Color.FromArgb(0, 128, 128)
        mColors(4) = Drawing.Color.FromArgb(128, 0, 0)
        mColors(5) = Drawing.Color.FromArgb(128, 0, 128)
        mColors(6) = Drawing.Color.FromArgb(128, 128, 0)
        mColors(7) = Drawing.Color.FromArgb(192, 192, 192)
        mColors(8) = Drawing.Color.FromArgb(128, 128, 128)
        mColors(9) = Drawing.Color.FromArgb(0, 0, 255)
        mColors(10) = Drawing.Color.FromArgb(0, 255, 0)
        mColors(11) = Drawing.Color.FromArgb(0, 255, 255)
        mColors(12) = Drawing.Color.FromArgb(255, 0, 0)
        mColors(13) = Drawing.Color.FromArgb(255, 0, 255)
        mColors(14) = Drawing.Color.FromArgb(255, 255, 0)
        mColors(15) = Drawing.Color.FromArgb(255, 255, 255)
    End Sub

    Public Sub SetColorChannel(colorName As String, channel As String, value As Byte)
        Dim index As Integer
        Select Case colorName
            Case "BLACK"
                index = 0
            Case "BLUE"
                index = 1
            Case "GREEN"
                index = 2
            Case "CYAN"
                index = 3
            Case "RED"
                index = 4
            Case "MAGENTA"
                index = 5
            Case "BROWN"
                index = 6
            Case "LGRAY"
                index = 7
            Case "DGRAY"
                index = 8
            Case "LBLUE"
                index = 9
            Case "LGREEN"
                index = 10
            Case "LCYAN"
                index = 11
            Case "LRED"
                index = 12
            Case "LMAGENTA"
                index = 13
            Case "YELLOW"
                index = 14
            Case "WHITE"
                index = 15
        End Select

        Dim oldcolor As Color = mColors(index)
        Select Case channel
            Case "R"
                mColors(index) = Drawing.Color.FromArgb(value, oldcolor.G, oldcolor.B)
            Case "G"
                mColors(index) = Drawing.Color.FromArgb(oldcolor.R, value, oldcolor.G)
            Case "B"
                mColors(index) = Drawing.Color.FromArgb(oldcolor.R, oldcolor.G, value)
        End Select
    End Sub

    Public Property Color(colorNumber As Integer) As Color
        Get
            Return mColors(colorNumber)
        End Get
        Set(value As Color)
            mColors(colorNumber) = value
        End Set
    End Property

    Private Function GetColorFromTriplet(token As Token, obj As RawObject, colorIndex As Integer, useBright As Boolean) As Color
        If token.Arguments.Count >= 3 Then
            Dim fColor As Integer = 0
            Integer.TryParse(token.Arguments(colorIndex), fColor)
            If useBright AndAlso token.Arguments(2) = "1" Then
                fColor += 8
            End If
            If fColor > 15 Then
                Return Drawing.Color.Transparent
            Else
                Return mColors(fColor)
            End If
        Else
            Return Drawing.Color.Black
        End If
    End Function

    Public Function GetBackColorFromTriplet(token As Token, obj As RawObject) As Color
        Return GetColorFromTriplet(token, obj, 1, False)
    End Function

    Public Function GetForeColorFromTriplet(token As Token, obj As RawObject) As Color
        Return GetColorFromTriplet(token, obj, 0, True)
    End Function

    Public Shared ReadOnly Property ColorName(index As Integer) As String
        Get
            Return mColorNames(index)
        End Get
    End Property
End Class
